import config from '../../config/index';
import { addContracts, getAreaAll } from '../../api/contracts'
Page({
  data: {
    showType: false,
    showRegion: false,
    showRentOut: false,
    type_list: ['整租', '合租', '短租'],
    region_list: [],
    person_name: '',
    person_phone: '',
    contracts_type: '整租',
    contracts_budget: '',
    house_type: '',
    house_region: '',
    contracts_content: '',
    rent_out_type: '整租',
    rent: ''
  },
  handleShowType() {
    this.setData({
      showType: !this.data.showType
    })
  },
  handleShowRentOut() {
    this.setData({
      showRentOut: !this.data.showRentOut
    })
  },
  handleShowRegion() {
    this.setData({
      showRegion: !this.data.showRegion
    })
  },
  onTypeChange(event: any) {
    const { value } = event.detail;
    this.setData({
      contracts_type: value
    })
    this.handleShowType()
  },
  onRentOutChange(event: any) {
    const { value } = event.detail
    this.setData({
      rent_out_type: value
    })
    this.handleShowRentOut()
  },
  onRegionChange(event: any) {
    const { value } = event.detail;
    this.setData({
      house_region: value
    })
    this.handleShowRegion()
  },
  async subFun() {
    let data: Contracts = {
      person_name: this.data.person_name,
      person_phone: this.data.person_phone,
      contracts_type: this.data.contracts_type,
      contracts_budget: this.data.contracts_budget,
      house_type: this.data.house_type,
      house_region: this.data.house_region,
      contracts_content: this.data.contracts_content,
      rent_out_type: this.data.rent_out_type,
      rent: this.data.rent
    }
    const res = await addContracts(data)
    if (res.statusCode === config.CREATED_CODE) {
      wx.showToast({
        title: '提交成功',
        icon: 'success'
      })
      const timer = setTimeout(() => {
        wx.navigateBack()
        clearTimeout(timer)
      }, 1000)
    }
  },

  async initData() {
    const res = await getAreaAll()
    if (res.statusCode === config.SUCCESS_CODE) {
      this.setData({
        region_list: res.data.map((item: DictItem) => {
          return item.item_value
        }),
        house_region: res.data[0].item_value
      })
    }
  },

  onLoad() {
    this.initData()
  }
})